package com.itheima.controller;

import com.itheima.dto.EmpDto;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.MyThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result getByPage(EmpDto dto){
        PageBean<Emp> pageBean = empService.getByPage(dto.getPage(), dto.getPageSize(), dto);
        Map<String,Object> value= MyThreadLocalUtils.getValue();
        System.out.println("从ThreadLocal中获取的令牌解析的结果是:==============" +
                "=================================="+value);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delByIds(@PathVariable Integer[] ids){
        int i=empService.deleteByIds(ids);
        return i>1?Result.success():Result.error(Arrays.toString(ids) +"删除失败");
    }

    @PostMapping
    public Result insertEmp(@RequestBody Emp emp){
        int i=empService.insertEmp(emp);
        return i==1?Result.success():Result.error("保存失败");
    }

}
